package com.devops.admin.controller;

import com.devops.admin.service.IpAddressService;
import com.devops.common.utils.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jxt
 * @Notes
 * @date 2024/6/26 16:14
 */
@RestController
@Api(tags = "ip相关",value = "/api/ip")
@RequestMapping("/api/ip")
@Slf4j
public class IPController {
    @Resource
    private IpAddressService ipAddressService;
    @GetMapping("/address")
    @ApiOperation(value="根据ip获取地址信息",notes = "根据ip获取地址信息")
    @ApiImplicitParam(name = "ip", value = "ip", required = true, dataType = "string", paramType = "query", example = "102.23.122.251")
    public Message getIpAddress(@RequestParam String ip){
        return Message.ok(ipAddressService.getIpAddress(ip));
    }

    @PostMapping("/address/batch")
    @ApiOperation(value="根据ip列表批量获取地址信息",notes = "根据ip列表批量获取地址信息")
    @ApiImplicitParam(name = "ips", value = "ips", required = true, dataType = "string", paramType = "body",
            example = "['102.23.122.251','47.106.201.134']")
    public Message getIpAddressList(@RequestBody List<String> ips){
        return Message.ok(ipAddressService.getIpAddressBatch(ips));
    }

    @PostMapping("/blocklist")
    @ApiOperation(value="添加ip黑名单",notes = "添加ip黑名单")
    @ApiImplicitParam(name = "ips", value = "ips", required = true, dataType = "string", paramType = "body",
            example = "['102.23.122.251','47.106.201.134']")
    public Message addBlocklist(@RequestBody List<String> ips){
        return Message.ok(ipAddressService.addBlocklist(ips));
    }

    @DeleteMapping("/blocklist")
    @ApiOperation(value="删除ip黑名单内ip",notes = "删除ip黑名单内ip")
    @ApiImplicitParam(name = "ips", value = "ips", required = true, dataType = "string", paramType = "body",
            example = "['102.23.122.251','47.106.201.134']")
    public Message removeBlocklist(@RequestBody List<String> ips){
        return Message.ok(ipAddressService.removeBlocklist(ips));
    }
}
